<template>
  <div class="main-all">
    <p><i class="el-icon-edit"></i>选题修改</p>
    <div class="main">
      <div class="main-1">
        <el-button icon="el-icon-plus" @click="dialogFormVisible = true; ADD()">新建</el-button>
        <el-checkbox-group v-model="checkList" style="display: flex;flex-direction: column;text-align: left;width: 460px;">
          <el-checkbox label="允许学生选课"></el-checkbox>
          <el-checkbox label="允许老师打分"></el-checkbox>
        </el-checkbox-group>
        <div style="display: flex;">
          <el-input style="margin-right: 20px; height: 30px; width: 100px;" v-model="inputCourse"
            placeholder="课程名"></el-input>
          <el-input style="margin-right: 20px; height: 30px; width: 100px;" v-model="inputStudent"
            placeholder="学生名"></el-input>
          <el-input style="margin-right: 20px; height: 30px; width: 100px;" v-model="inputClass"
            placeholder="班级名"></el-input>
          <el-button type="primary" @click="getPage" icon="el-icon-search">搜索</el-button>
        </div>
      </div>
      <el-pagination @current-change="fn2" :current-change="page" background layout="prev, pager, next" :total="total"
        style="margin-bottom: 10px;">
        <!--total页码，设定为变量 -->
      </el-pagination>

      <div style="overflow: scroll;width: 100%;height: 100%;">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="id" label="选课ID">
          </el-table-column>
          <el-table-column prop="courseName" label="课程名">
          </el-table-column>
          <el-table-column prop="studentName" label="学生名">
          </el-table-column>
          <el-table-column prop="className" label="学生班级">
          </el-table-column>
          <el-table-column prop="dailyScore" label="日常分">
          </el-table-column>
          <el-table-column prop="examScore" label="期末分">
          </el-table-column>
          <el-table-column prop="score" label="总分">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="dialogFormVisible = true; changed(scope.row)">编辑</el-button>
              <el-button type="text" @click=" deletes(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--  -->


      <el-dialog width="35%" title="编辑" :visible.sync="dialogFormVisible">
        <el-form :model="form" :label-width="formLabelWidth" :rules="rules">
          <el-form-item label="学生" prop="studentId">
            <el-select style="width: 100%;" v-model="form.studentId" placeholder="请选学生">
              <el-option v-for="(item, index) in tableDataStudents" :key="index" :label="item.name"
                :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程" prop="courseId">
            <el-select style="width: 100%;" v-model="form.courseId" placeholder="请选课程">
              <el-option v-for="(item, index) in tableDataClass" :key="index" :label="item.name"
                :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="日常得分" prop="dailyScore">
            <el-input-number :min="0" :max="100" style="width: 100%;" controls-position="right"
              v-model="form.dailyScore" autocomplete="off"></el-input-number>
          </el-form-item>
          <el-form-item label="考试得分" prop="examScore">
            <el-input-number :min="0" :max="100" style="width: 100%;" controls-position="right" v-model="form.examScore"
              autocomplete="off"></el-input-number>
          </el-form-item>
          <el-form-item label="总分" prop="score">
            <el-input-number :min="0" :max="100" style="width: 100%;" controls-position="right" v-model="form.score"
              autocomplete="off"></el-input-number>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false; ADDorChange()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>


</template>

<script>
import serve from '../api/index.js'
import { getPages, getList, ADDList, changeorno, deleted, getStudents, getClass, editList } from '../api/lesson-edit.js'
export default {
  data() {
    return {
      checkList:['允许学生选课','允许老师打分'],
      inputCourse: '',
      inputStudent: '',
      inputClass: '',
      // 搜索二人组
      total: 100,
      // 页码算数
      page: 1,
      // 页数
      tableData: [],
      // 课程编辑管理的res回复
      tableDataStudents: [],
      // 学生们的res回复
      tableDataClass: [],
      // 课程
      add: '',
      dialogFormVisible: false,
      form: {
        id: '',
        studentId: '',
        courseId: '',
        dailyScore: '',
        examScore: '',
        score: ''
      },
      formLabelWidth: '80px',
      rules: {
        name: [
          { required: true, message: '请输入课程', trigger: 'blur' },
        ],
        location: [
          { required: true, message: '请输入上课地点', trigger: 'blur' },
        ],
        grade: [
          { required: true, message: '请输入年级', trigger: 'blur' },
        ],
        examLocation: [
          { required: true, message: '请输入年级', trigger: 'blur' },
        ],
        //  department: [
        //   { required: true, message: '请输入系', trigger: 'blur' },
        // ],
      }
    }
  },
  methods: {
    getPage() {
      getPages(this.inputCourse, this.inputStudent, this.inputClass).then((res) => {
        console.log(res.data);
        this.total = res.data * 10
        if (res.message == 'success') {
          getList(this.page, this.inputCourse, this.inputStudent, this.inputClass).then((res) => {
            console.log(res);
            this.tableData = res.data
          })
          getStudents().then(res => {
            this.tableDataStudents = res.data
            console.log(this.tableDataStudents, 'this.tableDataStudents');
          })
          getClass().then(res => {
            this.tableDataClass = res.data
            console.log(this.tableDataClass, 'this.tableDataClass');
          })
        }
      })
    },//获取列表数据
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },//element
    fn2(val) {
      this.page = `${val}`
      console.log(this.page);
      this.getPage()
    },//获取页数

    fn() {
      // 获取 系ID--departmentId
      // this.tableDataAll.filter((item) => {
      //   if (item.name == this.form.department) {
      //     this.form.departmentId = item.id
      //     // console.log(this.form.departmentId, 'departmentId');
      //   }
      // })
    },
    ADD() {
      this.fn()
      this.OR = 'ADD'
      this.form.id = '-1'
      console.log(this.form);
    },//添加列表数据  

    changed(e) {
      this.OR = 'changed'
      console.log(e, 'e');
      editList(e.id).then(res => {
        console.log(res);
        this.form = res.data
      })
    },

    ADDorChange() {
      console.log(this.form);

      if (this.OR == 'ADD') {
        console.log(this.form);
        ADDList(this.form).then((res) => {
          console.log(res);
          this.getPage()
        })
      } else if (this.OR == 'changed') {
        changeorno(this.form).then((res) => {
          console.log(res);
          this.getPage()
        })
      }
    },

    // 删除
    deletes(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleted(id).then((res) => {
          console.log(res);
        })
        this.getPage()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      }); //删除列表数据   xxxx未完成

    }

  },
  mounted() {
    this.getPage()  //初始化列表数据
  }
}

</script>

<style scoped>
.el-table {
  width: 100% !important;
}

.main {
  .el-button {
    height: 40px;
  }

  .main-1 {
    height: 60px;
    display: flex;
    align-items: center;
    padding: 10px;
    justify-content: space-between;
  }


}
</style>
